package com.baihan.angus.app.game.busshow.pop_dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Typeface;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.StyleSpan;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.baihan.angus.app.game.basic.utils.AppUtil;
import com.baihan.angus.app.game.basic.utils.ComUtil;
import com.baihan.angus.app.game.busshow.R;
import com.qmuiteam.qmui.span.QMUITouchableSpan;
import com.qmuiteam.qmui.widget.textview.QMUISpanTouchFixTextView;

public class AgreePrivacyDialog extends Dialog {

    private Callback mCallback;
    private Context mContext;
    private WindowManager.LayoutParams mLayoutParams;
    private TextView tvNotUse, tvAgree;
    private QMUISpanTouchFixTextView tvTip;

    public AgreePrivacyDialog(@NonNull Context context, Callback mCallback) {
        super(context, R.style.FullDialog);
        this.mContext = context;
        this.mCallback = mCallback;
        initContentView();
        init(context);
    }

    private void init(Context context) {
        this.mContext = context;
        Window dialogWindow = this.getWindow();
        mLayoutParams = dialogWindow.getAttributes();
        mLayoutParams.width = AppUtil.getScreenWidth(context);
        mLayoutParams.height = ComUtil.Dp2Px(context, 470);
        dialogWindow.setAttributes(mLayoutParams);
        dialogWindow.getDecorView().setPadding(80, 0, 80, 0);
        dialogWindow.getDecorView().setBackgroundResource(android.R.color.transparent);
        dialogWindow.setGravity(Gravity.CENTER);
    }

    private void initContentView() {
        View dialogView = LayoutInflater.from(this.getContext()).inflate(R.layout.dialog_agee_privacy, (ViewGroup) null);
        setContentView(dialogView);
        tvTip = dialogView.findViewById(R.id.tvTip);
        tvNotUse = dialogView.findViewById(R.id.tvNotUse);
        tvAgree = dialogView.findViewById(R.id.tvAgree);

        tvTip.setMovementMethodDefault();
        tvTip.setNeedForceEventToParent(true);
        tvTip.setText(generateSp(mContext.getString(R.string.welcomeTips)));

        tvNotUse.setOnClickListener(view -> {
            if (null != mCallback) {
                mCallback.onNotUse();
            }
        });

        tvAgree.setOnClickListener(view -> {
            if (null != mCallback) {
                mCallback.onAgree();
            }
        });

    }

    //初始化 Span
    private SpannableString generateSp(String text) {
        String highlight1 = mContext.getString(R.string.serviceAgreement);
        String highlight2 = mContext.getString(R.string.privacyTips);
        SpannableString sp = new SpannableString(text);
        int start = 0, end;
        int index;
        while ((index = text.indexOf(highlight1, start)) > -1) {
            end = index + highlight1.length();
            sp.setSpan(new QMUITouchableSpan(mContext.getColor(R.color.textColor22), mContext.getColor(R.color.textColor22), 0, 0) {
                @Override
                public void onSpanClick(View widget) {
//                    ARouter.getInstance().build(ArouterAppPath.SINGLE_PAGE_ACTIVITY).withString("no", "eula").navigation();
                }
            }, index, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
            start = end;

            //加粗
            StyleSpan colorSpan = new StyleSpan(Typeface.BOLD);
            sp.setSpan(colorSpan, index, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        }

        start = 0;
        while ((index = text.indexOf(highlight2, start)) > -1) {
            end = index + highlight2.length();
            sp.setSpan(new QMUITouchableSpan(mContext.getColor(R.color.textColor22), mContext.getColor(R.color.textColor22), 0, 0) {
                @Override
                public void onSpanClick(View widget) {

                }
            }, index, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
            start = end;

            //加粗
            StyleSpan colorSpan = new StyleSpan(Typeface.BOLD);
            sp.setSpan(colorSpan, index, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        }

        return sp;
    }

    public interface Callback {
        void onNotUse();

        void onAgree();
    }
}
